<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>WordPress Comments &gt; Comments</title>
	<script type="text/javascript" src="../wp-includes/js/jquery/jquery.js"></script>
	<script type="text/javascript">
	<!--
		jQuery(function($) {
			
			var commentsClipboard = new Array()
			var documentTitle = document.title;
			
			// initiate getting a total count and get comments
			get_count()
			get_comments(10, 0)
			
			/******* GET COUNT **********
				Ajax call gets count of comments pending approval
				Updates both the document and document title
				Returns nothing
			****************************/
			function get_count(){
				$('h2').addClass('updating')
				$('#count').html('&nbsp;&nbsp;&nbsp;')
				$.ajax( { type: 'GET', url: 'comments.php', data: 'action=get_count',
					success: function(data){
						if ( !data ) data = '0'
						$('#count').html(' (' + data + ')')
						$('h2').removeClass('updating')
						document.title = '(' + data + ') ' + documentTitle;
					}, 
					error: function(XMLHttpRequest, textStatus, errorThrown){
						$('#count').replaceWith('<span id="count" class="error">(Unknown)</span>')
						$('h2').removeClass('updating')
						document.title = '(Unknown) ' + documentTitle;
					}
				});
			}
			
			/******* GET COMMENTS **********
				Ajax call gets pending comments
				Parameters: 
					number[int]: number of comments to get
					offset[int]: offset in case of paging
				Updates the comments list and turns links into Ajax calls
				Returns nothing
			****************************/
			function get_comments(number, offset){
				$('.commentform').remove()
				$('.reply').removeClass('selected')
				$('comments_msg').addClass('updating')
				$.ajax( { type: 'GET', url: 'comments.php', data: 'action=get_comments&number='+number+'&offset='+offset,
					success: function(data){
						$('#comments_msg').removeClass('updating')
						$('#comments_msg').html(data).fadeIn('fast')
						$('.actions a').bind('click', function(){
							change_comment_status($(this).attr('href'));
							return false
						});
						$('.reply').bind('click', function(){
							change_comment_status($(this).attr('href'));
							toggle_form($(this).attr('href'));
							return false
						});
					}, 
					error: function(XMLHttpRequest, textStatus, errorThrown){
						$('comments_msg').removeClass('updating')
						$('#comments_msg').replaceWith('<ol id="comments_msg"><li class="error">Unable to Get Comments.  '+textStatus+'</li></ol>')						
					}
				});
				
			}
			/******* TOGGLE FORM **********
				Toggles comment reply form
				Makes ajax call and inserts form provided by php script
				or hides form if form already open
				Returns false to disable original link click from executing
			****************************/
			function toggle_form(url_string){
				var comment = getParams(url_string)
				var div = '#c-'+comment.comment_parent;
				$('.commentform').remove()
				if ( $(div+' .reply').hasClass('selected') ) {
					$(div+' .reply').removeClass('selected') 
				}else{					
					$('.reply').removeClass('selected') 
					$(div+' .reply').addClass('updating')
					$.ajax( { type: 'GET', url: 'comments.php', data: 'action=comment_form&comment_parent='+comment.comment_parent+'&comment_post_id='+comment.comment_post_id,
						success: function(data){
							$(div+' .reply').removeClass('updating')
							if ( data ){
								$(div+' > .link-to-post').after(data)
								$(div+' .reply').addClass('selected')
								$(div+' .commentform').submit(function(){
									post_reply(div);
									return false
								});
							}else{
								$(div+' > .link-to-post').append('<div class="error">error creating form</div>')
							}
						}, 
						error: function(XMLHttpRequest, textStatus, errorThrown){
							$(div+' .reply').removeClass('updating')
							$(div+' > .link-to-post').append('<div class="error">error creating form</div>')
						}
					});
				}
				return false
			}

			/******* POST REPLY **********
				Makes an Ajax call to approve parent message
				Then makes an Ajax call to submit reply message
				Replaces list item with status of update and then refreshes list
				Returns nothing
			****************************/
			function post_reply(div){
				var form = div + ' .commentform';
				var comment_content = $(form+' #comment_content').attr('value');
				var comment_post_ID = $(form+' #comment_post_ID').attr('value');
				var comment_parent = $(form+' #comment_parent').attr('value');
				$(form).addClass('updating').html('Approving Message...<br />Submitting Reply...')
				// first approve message
				$(div + '.actions').remove()
				$.ajax( { type: 'GET', url: 'comments.php', data: 'action=approve&id='+comment_parent,
					// approve comment ajax success
					success: function(data){
						// approve comment success
						if ( data == 'Message Approved' ){
							$.ajax( { type: 'POST', url: 'comments.php', data: 'action=new_comment&comment_content'+comment_content+'&comment_post_ID='+comment_post_ID+'&comment_parent='+comment_parent,
								// submit reply ajax success
								success: function(data){
									$(form).removeClass('updating').slideUp('fast').html('Submitting Reply...').slideDown('fast')
									// submit reply success
									if ( data == 'Reply Posted' ){
										$(form).addClass('updated').html('Message Approved<br />Reply Posted').fadeTo(2000, 1.0, function () {
									        $(div).slideUp('fast').remove();
											get_count()
											get_comments(10, 0)
										});
									// submit reply error
									}else{
										$(form).removeClass('updating').html('<div class="error">Error Replying: '+data+'</div>')
										$(form + ' .error').fadeTo(2000, 1.0, function(){
											$(div).slideUp('fast').remove();
											get_count()
											get_comments(10, 0)
										});
									}
								}, 
								// submit reply ajax error
								error: function(XMLHttpRequest, textStatus, errorThrown){ 
									$(form).removeClass('updating').html('<div class="error">Error Approving: '+textStatus+'</div>')
									$(form + ' .error').fadeTo(2000, 1.0, function(){
										$(this).parent().slideUp('fast').remove();
										get_count()
										get_comments(10, 0)
									});
								}
							});
						// approve comment error
						}else{
							$(form).removeClass('updating').html('<div class="error">Error Approving: '+data+'<br />Unable to submit reply.</div>')
							$(form + ' .error').fadeTo(2000, 1.0, function(){
								$(this).parent().slideUp('fast').remove();
								get_count()
								get_comments(10, 0)
							});
						}
					},
					// approve comment ajax error
					error: function(XMLHttpRequest, textStatus, errorThrown){ 
						$(form).removeClass('updating').html('<div class="error">Error Approving: '+textStatus+'<br />Unable to submit reply.</div>')
						$(form + ' .error').fadeTo(2000, 1.0, function(){
							$(this).parent().slideUp('fast').remove();
							get_count()
							get_comments(10, 0)
						});
					}
				});
			}
			
			/******* Change Comment Status **********
				Makes an Ajax call to change comment status
				Parameter: url_sting[string]: url in the format of pathurl?action=approve&id=28
				Replaces list item with status of update and then refreshes list
			****************************/
			function change_comment_status(url_sting){
				var comment = getParams(url_sting)
				var div = '#c-'+comment.id;
				commentsClipboard[comment.id] = $(div)
				$(div).addClass('updating').slideUp('fast').html('Updating...').slideDown('fast')
				$.ajax( { type: 'GET', url: 'comments.php', data: 'action=approve&id='+comment.id,
					success: function(data){
						$(div).removeClass('updating')
						if ( data=='Message Approved' || data=='Message Marked as Spam' || data=='Message Deleted'){
							$(div).html(data).addClass('updated').fadeTo(2000, 1.0,  function () {
						        $(this).slideUp('fast').remove();
								get_count()
								get_comments(10, 0)
							});
						}else{
							$(div).html('<div class="error">'+data+'</div>')
							$(div + ' .error').fadeTo(2000, 1.0, function(){
								$(this).parent().slideUp('fast').remove();
								get_count()
								get_comments(10, 0)
							});
						}
					}, 
					error: function(XMLHttpRequest, textStatus, errorThrown){
						$(div).removeClass('updating').html('<div class="error">Error Updating Message Status: '+textStatus+'</div>')
						$(div + ' .error').fadeTo(2000, 1.0, function(){
							$(this).parent().slideUp('fast').remove();
							get_count()
							get_comments(10, 0)
						});
					}
				});
			}
			
			/******* GET PARAMETERS **********
				Returns associative array of parameters from a url
				Returns false if nothing found beyond the ? character
			****************************/
			function getParams(str){
				if ( str.indexOf("?") > -1 ){
					var paramArray = new Object;
					paramArray.url = str.substr(0, str.indexOf("?"));
					var query = str.substr(str.indexOf("?")+1).toLowerCase();
					var queryArray = query.split("&");
					for ( var i=0; i<queryArray.length; i++ ){
						param = queryArray[i].split("=");
						paramArray[param[0]] = param[1]
				  	}
					return paramArray
				}else{
					return false
				}
			}
		});
	-->
    </script>
	<link rel="stylesheet" type="text/css" href="style.css" />
	<link media="handheld, only screen and (max-device-width: 480px)" 
		    href="iphone.css" type="text/css" rel="stylesheet" />
	<meta name="viewport" content="width=320; initial-scale=1.0;" />
	</head>
<body>
	<div id="wrapper">
		<h1>WordPress Comments</h1>
		<h2 id="count_msg"><span id="count">&nbsp;&nbsp;&nbsp;</span> Messages Pending Approval</h2>
		<div>
			<ol id="comments_msg">
				<li>Getting Comments...</li>
			</ol>
		</div>
		
	</div>
</body>
</html>
